﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System;
using System.Collections.Generic;
using System.Reflection;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.FieldRef"/>.
    /// </summary>
    public class FieldRefVM : VariableRefVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(FieldRef),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new FieldRefVM((FieldRef)codeObject, dictionary); });
        }

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.FieldRef"/>.
        /// </summary>
        public FieldRefVM(FieldRef fieldRef, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(fieldRef, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.FieldRef"/> model.
        /// </summary>
        public FieldRef FieldRef
        {
            get { return (FieldRef)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public static void RenderFieldInfo(CodeRenderer renderer, FieldInfo fieldInfo, RenderFlags flags, CodeObjectVM tag)
        {
            RenderFlags passFlags = flags & ~RenderFlags.Description;
            bool hasBorder = flags.HasFlag(RenderFlags.ForceBorder);
            if (hasBorder)
                renderer.CreateBorder(VariableDeclVM.StaticBorderBrush, VariableDeclVM.StaticBackgroundBrush, tag);

            // Skip all details for enum members, including the declaring type since it will always be on the left of the dot
            if (!(fieldInfo.IsLiteral && fieldInfo.FieldType.IsEnum))
            {
                if (!flags.HasFlag(RenderFlags.NoPreAnnotations))
                    AttributeVM.RenderAttributes(renderer, fieldInfo, tag);
                renderer.RenderText(ModifiersHelpers.AsString(FieldRef.GetFieldModifiers(fieldInfo)), KEYWORD_BRUSH, tag);
                Type fieldType = fieldInfo.FieldType;
                TypeRefBaseVM.RenderTypeAsTypeRefVM(renderer, fieldType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                renderer.RenderText(" ", PUNC_BRUSH, tag);
                TypeRefBaseVM.RenderTypeAsTypeRefVM(renderer, fieldInfo.DeclaringType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                DotVM.RenderDot(renderer, tag);
            }
            renderer.RenderText(fieldInfo.Name, IDENTIFIER_BRUSH, tag);

            // If we have a border, return to the previous one
            if (hasBorder)
                renderer.ReturnToBorderParent(tag);
        }

        #endregion
    }
}
